<?php
/*配送模块*/
namespace SDXApi\Controller;
use SDXApi\Service\OrderDistributionTimeService;
use Think\Controller;
class InventoryController extends AppController {
	public function _initialize($apptoken = '', $apptime = '', $check_uid = false, $session_id = '',$check_sign=true)
	{
		parent::_initialize($apptoken, $apptime, $check_uid, $session_id,false,false);
	}
	
	
	/**
	 * 筛选现售订单商品，进行减相应的实际库存，并且记录
	 * */
	public function save_ordergoods_storage(){
	    $order_id=I('post.order_id');
	    $wid=I('post.wid');
	    $user_id=I('post.user_id');
	    if(empty($order_id)){
	        returnApiError('订单ID不能为空');
	    }
	    if(empty($wid)){
	        returnApiError('仓库ID不能为空');
	    }
	    if(empty($user_id)){
	        returnApiError('用户ID不能为空');
	    }
	    $ordergoods=D('Orderinfo')->get_ordergoods($order_id,0);//搜现售订单商品，有就减相应的实际库存，并且记录
	    if($ordergoods){
	        $ordergoods_old=$ordergoods;
	        $goodsstorage_mod=D('Goodsstorage');
	        $flas=false;
	        $errgoods=array();
	        //该笔订单检查订单商品库存数是否充足
	        foreach ($ordergoods_old as $key=>$val){
	            $practical_num=$goodsstorage_mod->check_warehous_goods_return($wid,$val['goods_id'],$val['goods_number']);
	            if($practical_num!==false){
	                if(empty($practical_num)){
	                    $practical_num=0;
	                }
	                $flas=true;
	                $val['practical_num']=$practical_num;
	                $errgoods[]=$val;
	            }
	        }
	        if($flas){
	            returnApiError('此订单里面某些商品存在不足',array('errgoods'=>$errgoods));
	        }
	        foreach ($ordergoods as $k=>$v){
	            //减去实际库存
	            $goodsstorage_mod->save_theactual_goods($wid,$v['goods_id'],$v['goods_number'],true);
	            $goodsstorage=$goodsstorage_mod->get_warehous_goods($wid,$v['goods_id']);
	            if($goodsstorage){
	                $goodsrecord=array(
	                    'goods_storage_id'=>$goodsstorage['id'],
	                    'wpid'=>0,
	                    'wid'=>$goodsstorage['wid'],
	                    'goods_id'=>$v['goods_id'],
	                    'goods_sn'=>$v['goods_sn'],
	                    'operation_num'=>$v['goods_number'],
	                    'real_num'=>$goodsstorage['practical_num'],
	                    'frozen_num'=>0,
	                    'defective_num'=>0,
	                    'operation_userid'=>0,
	                    'average_userid'=>$user_id,
	                    'operation_addtime'=>gmtime(),
	                    'source_no'=>$v['order_sn'],
	                    'record_type'=>10,
	                    'remark'=>'商品下单',
	                );
	                //商品出入库记录
	                D('Goodsstoragerecord')->add_storage_record($goodsrecord);
	                $goodsfreezerecord=array(
	                    'goods_storage_id'=>$goodsstorage['id'],
	                    'wid'=>$goodsstorage['wid'],
	                    'goods_id'=>$v['goods_id'],
	                    'goods_sn'=>$v['goods_sn'],
	                    'goods_name'=>$v['goods_name'],
	                    'freeze_num'=>$v['goods_number'],
	                    'real_num'=>0,
	                    'frozen_num'=>$goodsstorage['freeze_num'],
	                    'defective_num'=>0,
	                    'operation_userid'=>0,
	                    'average_userid'=>$user_id,
	                    'operation_addtime'=>gmtime(),
	                    'source_no'=>$v['order_sn'],
	                    'record_type'=>4,
	                    'remark'=>'商品下单',
	                );
	                //商品冻结出入记录
	                D('Goodsfreezerecord')->add_freeze_record($goodsfreezerecord);
	            }
	        }
	        returnApiSuccess(array('id'=>''),'success');
	    }else{
	        returnApiError('进行减相应的实际库存失败');
	    }
	}
	
	/* 减实际冻结库存数*/
	public function save_ordergoods_freeze(){
	    $order_id=I('post.order_id');
	    $wid=I('post.wid');
	    $user_id=I('post.user_id');
	    if(empty($order_id)){
	        returnApiError('订单ID不能为空');
	    }
	    if(empty($wid)){
	        returnApiError('仓库ID不能为空');
	    }
	    if(empty($user_id)){
	        returnApiError('用户ID不能为空');
	    }
	    $ordergoods=D('Orderinfo')->get_ordergoods($order_id,0);//搜现售订单商品，有就减相应的实际冻结库存，并且记录
	    if($ordergoods){
	        $goodsstorage_mod=D('Goodsstorage');
	        foreach ($ordergoods as $k=>$v){
	            //减去实际冻结库存
	            $goodsstorage_mod->save_freeze_theactual_goods($wid,$v['goods_id'],$v['goods_number']);
	            $goodsstorage=$goodsstorage_mod->get_warehous_goods($wid,$v['goods_id']);
	            if($goodsstorage){
	                $goodsfreezerecord=array(
	                    'goods_storage_id'=>$goodsstorage['id'],
	                    'wid'=>$goodsstorage['wid'],
	                    'goods_id'=>$v['goods_id'],
	                    'goods_sn'=>$v['goods_sn'],
	                    'goods_name'=>$v['goods_name'],
	                    'freeze_num'=>$v['goods_number'],
	                    'real_num'=>0,
	                    'frozen_num'=>$goodsstorage['freeze_num'],
	                    'defective_num'=>0,
	                    'operation_userid'=>0,
	                    'average_userid'=>$user_id,
	                    'operation_addtime'=>gmtime(),
	                    'source_no'=>$v['order_sn'],
	                    'record_type'=>6,
	                    'remark'=>'确认收货',
	                );
	                //商品冻结出入记录
	                D('Goodsfreezerecord')->add_freeze_record($goodsfreezerecord);
	            }
	        }
	        returnApiSuccess(array('id'=>''),'success');
	    }else{
	        returnApiError('进行减相应的冻结库存失败');
	    }
	}
	
	/*检查对应仓库库存商品数是否为0*/
	public function check_warehous_goods(){
	    $wid=I('post.wid');
	    $goods_id=I('post.goods_id');
	    $num=I('post.num');
	    if(empty($wid)){
	        returnApiError('仓库ID不能为空');
	    }
	    if(empty($goods_id)){
	        returnApiError('商品ID不能为空');
	    }
	    if(empty($num)){
	        $num=1;
	    }
	    $id=D('Goodsstorage')->check_warehous_goods($wid,$goods_id,$num);
	    if($id){
	        returnApiSuccess(array('id'=>$id),'有库存');
	    }else{
	        returnApiError('该仓库商品没有库存');
	    }
	}
	
	/*系统盘点*/
	public function system_pandian(){
	    $wid=I('post.wid');
	    $goods_id=I('post.goods_id');
	    $pandian=D('Goodsstorage')->system_freeze_pandian($goods_id,$wid);
	    if($pandian){
	        returnApiSuccess(array('id'=>1),'恭喜系统盘点成功');
	    }else{
	        returnApiError('系统盘点错误');
	    }
	}
	
	/*系统盘点,针对收货确认没有减冻结的情况*/
	public function system_pandian_confirm(){
	    $wid=I('post.wid');
	    $goods_id=I('post.goods_id');
	    $pandian=D('Goodsstorage')->system_freeze_pandian_confirm($goods_id,$wid);
	    if($pandian){
	        returnApiSuccess(array('id'=>1),'恭喜系统盘点成功');
	    }else{
	        returnApiError('系统盘点错误');
	    }
	}
	
	/*批量调仓请求*/
	public function batch_warehouse_request(){
	    $wid=I('post.wid');
	    $batch_warehouse=D('Thewarehouseacceptconfig')->batch_warehouse_request_goods($wid);
	    if($batch_warehouse){
	        returnApiSuccess(array('id'=>1));
	    }else{
	        returnApiError('批量调仓请求失败');
	    }
	}
	
	/*
	 * 门店处理存储过程
	 * */
	public function store_shift(){
	    $user_id=I('post.user_id','intval');
	    $v1=I('post.v1');
	    $v2=I('post.v2');
	    if(!$user_id || empty($v1) || empty($v2)){
	        returnApiError('参数错误');
	    }
	    //$res=M()->query(" call pro_store_shift('".$user_id."',".$v1.",".$v2.")");
	    $res=M()->query(" call pro_store_shift_new('".$user_id."',".$v1.",".$v2.")");
	    if($res){
	        returnApiSuccess(array('data'=>$res));
	    }else{
	        returnApiError('查无数据');
	    }
	}
	
	/*
	 * 门店充值
	 * */
	public function store_recharge(){
	    $user_id=I('post.user_id','intval');
	    $amount=I('post.amount');
	    if(!$user_id || empty($amount)){
	        returnApiError('参数错误');
	    }
	    D('Payment')->recharge_bonus($user_id,$amount);
	    returnApiSuccess(array('id'=>1));
	}

    /*
     * 订单自动取消接口
     * */
    public function auto_order_cancel(){
        ini_set('max_execution_time', '0');
        set_time_limit(0);
        $exec_time=I('get.exec_time');// 已分钟为单位执行时间。默认立即执行
        if(empty($exec_time)){
            $exec_time=1;
        }
        //$exec_time1=$exec_time*2;
        $update_ordersql="UPDATE ".C('DB_PREFIX')."order_info AS t".
            //" JOIN ".C('DB_PREFIX')."pay_log AS o ON o.order_id=t.order_id".
            " SET t.order_status=2,t.pay_note='支付超时自动取消'".
            " WHERE t.order_status=0 AND t.pay_status=0 AND t.shipping_status=0 AND TIMESTAMPDIFF(MINUTE,CONVERT_TZ(FROM_UNIXTIME(IF(t.pay_start_time=0,t.add_time,t.pay_start_time)),'+00:00','+08:00'),NOW())>".$exec_time;
        M('')->execute($update_ordersql);
        $select_ordersql="SELECT og.goods_id,og.goods_sn,og.goods_name,og.goods_number,og.is_presell,og.buckle_inventory,oi.wid,oi.user_id,oi.order_sn".
            " FROM ".C('DB_PREFIX')."order_info oi".
            " LEFT JOIN ".C('DB_PREFIX')."order_goods og ON oi.order_id=og.order_id".
            " WHERE oi.order_status=2 AND pay_status=0 AND oi.pay_note='支付超时自动取消'";
        $order_goodslist=M('')->query($select_ordersql);
        if($order_goodslist){
            $goodsstorage_mod=D("Goodsstorage");
            $goods_mod=M('goods');
            foreach ($order_goodslist as $key=>$val){
                $where=array('goods_id'=>$val['goods_id']);
                $goods_number=$goods_mod->where($where)->getField('goods_number');
                $goods_mod->where($where)->save(array('goods_number'=>$goods_number+$val['goods_number']));
                if($val['is_presell']==0 || ($val['is_presell']==1 && $val['buckle_inventory']==1)){
                    $goodsstorage_list=$goodsstorage_mod->get_warehous_goods($val['wid'],$val['goods_id']);
                    //获取库存缓存
                    $cache_goods_practical_num = get_goods_practical_num_cache($val['goods_id'],$val['wid'],$goodsstorage_list['practical_num']);
                    //获取队列库存缓存
                    $cache_queue_goods_stock = get_queue_goods_stock($val['goods_id'],$val['wid']);
                    if($goodsstorage_list){
                        //核实实际库存出入库记录是否有商品下单记录-的操作，若有则返回true，否则返回false
                        $vgsr=$goodsstorage_mod->verify_gsr($val['wid'],$val['goods_id'],$val['order_sn']);
                        //核实冻结库存出入库记录是否有商品下单记录+的操作，若有则返回true，否则返回false
                        $vgfr=$goodsstorage_mod->verify_gfr($val['wid'],$val['goods_id'],$val['order_sn']);
                        $practical_num=$goodsstorage_list['practical_num']+$val['goods_number'];
                        $freeze_num=$goodsstorage_list['freeze_num']-$val['goods_number'];
                        $updata_gsdata=array();
                        if($vgsr){
                            $updata_gsdata['practical_num']=$practical_num;
                        }
                        if($vgfr){
                            $updata_gsdata['freeze_num']=$freeze_num;
                        }
                        if($vgsr || $vgfr){
                            //总库存中 实际库存数+  冻结库存数-
                            $flag = M('goods_storage')->where(array('goods_id'=>$val['goods_id'],'wid'=>$val['wid']))->save($updata_gsdata);
                            unset($updata_gsdata);
                            //更新库存缓存
                            if($flag!==false){
                                set_goods_practical_num_cache($val['goods_id'],$val['wid'],$cache_goods_practical_num+$val['goods_number']);
                            }
                        }
                        if($vgsr){
                            $goodsrecord=array(
                                'goods_storage_id'=>$goodsstorage_list['id'],
                                'wpid'=>0,
                                'wid'=>$val['wid'],
                                'goods_id'=>$val['goods_id'],
                                'goods_sn'=>$val['goods_sn'],
                                'operation_num'=>$val['goods_number'],
                                'real_num'=>$practical_num,
                                'frozen_num'=>0,
                                'defective_num'=>0,
                                'operation_userid'=>0,
                                'average_userid'=>$val['user_id'],
                                'operation_addtime'=>gmtime(),
                                'source_no'=>$val['order_sn'],
                                'record_type'=>6,
                                'remark'=>'订单取消',
                            );
                            //商品出入库记录

                            D('Goodsstoragerecord')->add_storage_record($goodsrecord);
                            //unset($goodsrecord);
                            //set_queue_goods_stock($val['goods_id'],$val['wid'],$cache_queue_goods_stock+$val['goods_number']);
                        }
                        if($vgfr){
                            $goodsfreezerecord=array(
                                'goods_storage_id'=>$goodsstorage_list['id'],
                                'wid'=>$val['wid'],
                                'goods_id'=>$val['goods_id'],
                                'goods_sn'=>$val['goods_sn'],
                                'goods_name'=>$val['goods_name'],
                                'freeze_num'=>$val['goods_number'],
                                'real_num'=>0,
                                'frozen_num'=>$freeze_num,
                                'defective_num'=>0,
                                'operation_userid'=>0,
                                'average_userid'=>$val['user_id'],
                                'operation_addtime'=>gmtime(),
                                'source_no'=>$val['order_sn'],
                                'record_type'=>5,
                                'remark'=>'订单取消',
                            );
                            //商品冻结出入记录
                            D('Goodsfreezerecord')->add_freeze_record($goodsfreezerecord);
                            unset($goodsfreezerecord);
                        }
                    }
                }
                unset($goodsstorage_list);
            }
        }
        unset($order_goodslist);
        $order_actionsql="select order_id,order_sn,order_status,shipping_status,pay_status,shipping_id,196_admin_id,wdr_id,best_time_start,best_time".
            " FROM ".C('DB_PREFIX')."order_info".
            " where order_status=2 AND pay_status=0 AND pay_note='支付超时自动取消'";
        $order_actiondata=M('')->query($order_actionsql);
        if($order_actiondata){
            $orderaction_mod=D('OrderAction');
            $distribution = new OrderDistributionTimeService();
            foreach ($order_actiondata as $oakey=>$oaval){
                $data=array(
                    'order_id'=>$oaval['order_id'],
                    'action_user'=>'订单取消-自动脚本',
                    'order_status'=>$oaval['order_status'],
                    'shipping_status'=>$oaval['shipping_status'],
                    'pay_status'=>$oaval['pay_status'],
                    'action_note'=>'支付超时自动取消',
                    'log_time'=>gmtime(),
                );
                $orderaction_mod->insert_order_action($data);
                unset($data);
                //$this->check_ordergoods_is_cancel($oaval['order_sn']);
                //添加当前区域的时间段订单量缓存 减一
                if($oaval['shipping_id']==3 && $oaval['196_admin_id']==0){
                    $distribution->add_order_best_time_cache($oaval['wdr_id'], $oaval['best_time_start'], $oaval['best_time'], $oaval['order_id'], -1);
                }
            }
            unset($orderaction_mod);

        }
        unset($order_actiondata);
        $select_orderinfosql="select o.order_id from ".C('DB_PREFIX')."order_info o where o.bonus_id !=0 AND o.order_status=2 AND o.pay_status=0 AND o.pay_note='支付超时自动取消'";
        $order_infolist=M('')->query($select_orderinfosql);
        if($order_infolist){
            foreach ($order_infolist as $k=>$v){
                $updata_user_bonussql="UPDATE ".C('DB_PREFIX')."user_bonus t".
                    " SET t.used_time=0,t.order_id=0 where t.order_id=".$v['order_id'];
                M('')->execute($updata_user_bonussql);
            }
        }
		$updata_order_infosql="UPDATE ".C('DB_PREFIX')."order_info SET to_buyer=pay_note,pay_note='', money_paid=0, order_amount=0, surplus=0 WHERE order_status=2 AND pay_status=0 AND pay_note='支付超时自动取消'";
        M('')->execute($updata_order_infosql);
        $del_activity_quotasql="DELETE FROM ".C('DB_PREFIX')."activity_quota WHERE order_id IN(SELECT order_id FROM ".C('DB_PREFIX')."order_info WHERE order_status=2)";
        M('')->execute($del_activity_quotasql);

        $where_o['order_status'] = 2;
        $where_o['bonus_freight_id'] = array("neq",0);
        $where_o['pay_status'] = 0;
        $where_o['to_buyer'] = '支付超时自动取消';
        $bonus_freight_array = M("order_info")->where($where_o)->getField("bonus_freight_id",true);
        M("order_info")->where($where_o)->setField("bonus_freight_id",0);

        if($bonus_freight_array){
            $where_sq['bonus_id'] = array("in",$bonus_freight_array);
            $data['used_time'] = 0;
            $data['order_id'] = 0;
            $data['pay_log_id'] = 0;

            M("user_bonus")->where($where_sq)->save($data);
        }
        //w_log("bonus_freight_array",implode(",",$bonus_freight_array));*/

    }
	
	/*
	 * 检查冻库记录是否存在商品下单商品+，若有怎必须有对应订单取消-操作
	 * */
	private function check_ordergoods_is_cancel($order_sn=''){
	    ini_set('max_execution_time', '0');
	    set_time_limit(0);
	    $gfrmod=M('goods_freeze_record');
	    $gsrmod=M('goods_storage_record');
	    $where=array('source_no'=>$order_sn);
	    $gfrlist=$gfrmod->field('id,goods_storage_id,wid,goods_id,goods_sn,freeze_num,source_no,record_type,average_userid')->where($where)->select();
	    if($gfrlist){
	        $plus=0; // 商品下单+
	        $minus=0;// 订单取消-
	        $plusdata=array();
	        foreach ($gfrlist as $k=>$v){
	            if($v['record_type']==4){
	                $plus=$plus+1;
	                $plusdata[]=$v;
	            }else if($v['record_type']==5){
	                $minus=$minus+1;
	            }
	        }
	        if($plus!=$minus && count($plusdata)>0){
	               $goodsstorage_mod=D("Goodsstorage");
	               foreach ($plusdata as $key=>$val){
	                   $goodsstorage_list=$goodsstorage_mod->get_warehous_goods($val['wid'],$val['goods_id']);
	                   //获取库存缓存
	                   $cache_goods_practical_num = get_goods_practical_num_cache($val['goods_id'],$val['wid'],$goodsstorage_list['practical_num']);
	                   //获取队列库存缓存
	                   $cache_queue_goods_stock = get_queue_goods_stock($val['goods_id'],$val['wid']);
	                   if($goodsstorage_list){
	                       $updata_gsdata=array();
	                       $gsrid=$gsrmod->field('id')->where(array('wid'=>$val['wid'],'goods_id'=>$val['goods_id'],'record_type'=>6,'source_no'=>$val['source_no']))->find();
	                       if(empty($gsrid['id'])){
	                           $practical_num=$goodsstorage_list['practical_num']+$val['freeze_num'];
	                           $updata_gsdata['practical_num']=$practical_num;
	                           $goodsrecord=array(
	                               'goods_storage_id'=>$goodsstorage_list['id'],
	                               'wpid'=>0,
	                               'wid'=>$val['wid'],
	                               'goods_id'=>$val['goods_id'],
	                               'goods_sn'=>$val['goods_sn'],
	                               'operation_num'=>$val['freeze_num'],
	                               'real_num'=>$practical_num,
	                               'frozen_num'=>0,
	                               'defective_num'=>0,
	                               'operation_userid'=>0,
	                               'average_userid'=>$val['user_id'],
	                               'operation_addtime'=>gmtime(),
	                               'source_no'=>$val['source_no'],
	                               'record_type'=>6,
	                               'remark'=>'订单取消',
	                           );
	                           //商品出入库记录
	                           D('Goodsstoragerecord')->add_storage_record($goodsrecord);
	                           unset($goodsrecord);
	                           set_queue_goods_stock($val['goods_id'],$val['wid'],$cache_queue_goods_stock+$val['freeze_num']);
	                       }
	                       $gfrid=$gfrmod->field('id')->where(array('wid'=>$val['wid'],'goods_id'=>$val['goods_id'],'record_type'=>5,'source_no'=>$val['source_no']))->find();
	                       if(empty($gfrid['id'])){
	                           $freeze_num=$goodsstorage_list['freeze_num']-$val['freeze_num'];
	                           $updata_gsdata['freeze_num']=$freeze_num;
	                           $goodsfreezerecord=array(
	                               'goods_storage_id'=>$goodsstorage_list['id'],
	                               'wid'=>$val['wid'],
	                               'goods_id'=>$val['goods_id'],
	                               'goods_sn'=>$val['goods_sn'],
	                               'goods_name'=>$val['goods_name'],
	                               'freeze_num'=>$val['freeze_num'],
	                               'real_num'=>0,
	                               'frozen_num'=>$freeze_num,
	                               'defective_num'=>0,
	                               'operation_userid'=>0,
	                               'average_userid'=>$val['user_id'],
	                               'operation_addtime'=>gmtime(),
	                               'source_no'=>$val['source_no'],
	                               'record_type'=>5,
	                               'remark'=>'订单取消',
	                           );
	                           //商品冻结出入记录
	                           D('Goodsfreezerecord')->add_freeze_record($goodsfreezerecord);
	                           unset($goodsfreezerecord);
	                       }
	                       
	                       if(count($updata_gsdata)>0){
	                           //总库存中 实际库存数+  冻结库存数-
	                           $flag = M('goods_storage')->where(array('goods_id'=>$val['goods_id'],'wid'=>$val['wid']))->save($updata_gsdata);
	                           unset($updata_gsdata);
	                           //更新库存缓存
	                           if($flag!==false){
	                               set_goods_practical_num_cache($val['goods_id'],$val['wid'],$cache_goods_practical_num+$val['freeze_num']);
	                           }
	                       }
	                       
	                       
	                   }
	                   
	               }
	               unset($plusdata);
	        }
	        
	    }
	}
	
	/*
	 * 针对冻结库存系统重置操作
	 * */
	public function system_freeze_reset(){
	    $wid=I('post.wid');
	    $goods_id=I('post.goods_id');
	    if(empty($wid)){
	        returnApiError('参数错误');
	    }
	    $goods_storage_mod=M('goods_storage');
	    $gswhere='';
	    if(!empty($wid)){
	        $gswhere.='wid='.$wid;
	    }
	    if(!empty($goods_id)){
	        $gswhere.=' and goods_id='.$goods_id;
	    }
	    $gslist=$goods_storage_mod->where($gswhere)->select();
	    if($gslist){
	        $source_no=$this->get_batch_no('CZ');
	        foreach ($gslist as $key=>$val){
	            $gsid=$goods_storage_mod->where(array('wid'=>$val['wid'],'goods_id'=>$val['goods_id']))->save(array('freeze_num'=>0));
	            if($gsid!==false){
	            $goodsfreezerecord=array(
                            'goods_storage_id'=>$val['id'],
                            'wid'=>$val['wid'],
                            'goods_id'=>$val['goods_id'],
                            'goods_sn'=>$val['goods_sn'],
                            'goods_name'=>$val['goods_name'],
                            'freeze_num'=>$val['freeze_num'],
                            'real_num'=>0,
                            'frozen_num'=>0,
                            'defective_num'=>0,
                            'operation_userid'=>0,
                            'average_userid'=>0,
                            'operation_addtime'=>gmtime(),
                            'source_no'=>$source_no,
                            'record_type'=>16,
                            'remark'=>'系统重置',
                 );
                 //商品冻结出入记录
                 D('Goodsfreezerecord')->add_freeze_record($goodsfreezerecord);
	            }
	        }
	        returnApiError(array('id'=>1),'执行成功');
	    }else{
	        returnApiError('查无数据');
	    }
	}
	
	/**
	 * 得到新订单号
	 * @return  string
	 */
	private function get_batch_no($prefix) {
	    /* 选择一个随机的方案 */
	    mt_srand((double) microtime() * 1000000);
	
	    return $prefix.date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
	}
	
}